import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
 

public class FileManager {
    
	

	public void writeToFile(Student student) throws IOException
	{
		File file = new File("student.txt");
		PrintWriter writer  = new PrintWriter(new BufferedWriter(new FileWriter(file,true)));
		writer.append(student.getName());
		writer.append("#");
		writer.append(student.getSurname());
		writer.append("#");
		writer.append(student.getStudNumber());
		writer.append("#");
		writer.append(student.getCourse());
		writer.append("#");
		writer.append(student.getDepartment());
		writer.append("#");
		writer.append(student.getAddress());
		writer.append("\n");
		writer.close();
		
	}
	
	public ArrayList<Student> readFromFile() throws IOException
	{
		File file = new File("student.txt");
		BufferedReader reader = new BufferedReader(new FileReader(file));
		ArrayList<Student> students = new ArrayList<Student>();
		Student student = new Student();
		String line = "";
		while((line = reader.readLine()) != null)
		{
			StringTokenizer token = new StringTokenizer(line, "#");
			while(token.hasMoreElements())
			{
				student = new Student();
				student.setName(token.nextElement().toString());
				student.setSurname(token.nextElement().toString());
				student.setStudNumber(token.nextElement().toString());
				student.setCourse(token.nextElement().toString());
				student.setDepartment(token.nextElement().toString());
				student.setAddress(token.nextElement().toString());
			}
			
			students.add(student);
		}
		return students;
	}
	
}

